package com.abocode.aichat.core.application.impl;

import com.abocode.aichat.core.application.UserService;
import com.abocode.aichat.core.domain.entity.BlackList;
import com.abocode.aichat.core.domain.entity.User;
import com.abocode.aichat.core.domain.repository.BlackListRepository;
import com.abocode.aichat.core.domain.repository.UserRepository;
import com.abocode.aichat.core.interfaces.constant.ApplicationCostant;
import com.abocode.aichat.server.definition.UserValidator;
import com.abocode.aichat.server.entity.Entity;
import com.abocode.aichat.server.entity.EntityImpl;
import com.abocode.aichat.server.exception.EntityFormatException;
import com.abocode.aichat.utils.ValidateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import java.util.Map;

/**
 * Created by guanxf on 2015/8/5.
 */

@Service("userValidateService")
public class UserValidateService implements UserValidator,UserService {
    @Autowired
    private BlackListRepository blackListRepository;
    @Autowired
    private UserRepository userRepository;
    @Value("${im.domain}")
    private  String domain;
    /***
     * 查询用户信息
     * @param entity
     * @return
     */
    private User getUserInfo(Entity entity) {
        User user=  userRepository.findOneByPhoneNumber(entity.getNode());
        return  user;
    }


    /**
     * 获取用户状态
     * @param entity
     * @return
     */
    private String getStatus(Entity entity) {
        return  this.getUserInfo(entity).getStatus();
    }

    @Override
    public boolean validate(Object o) {
        boolean isPass=true;
        Map map=(Map)o;
        String uid=String.valueOf(map.get("uid"));
        if(!ValidateUtils.isLegitimacyNo(uid)){
            isPass=false;
        }
        return isPass;
    }

    @Override
    public EntityImpl entity(Object o) {
        Map body = (Map) o;
        String uid = String.valueOf(body.get("uid"));
        String os = String.valueOf(body.get("os"));
        String token = String.valueOf(body.get("token"));
        String presence = String.valueOf(body.get("presence"));
        if(StringUtils.isEmpty(domain)){
            throw new EntityFormatException("domain not config");
        }

       /*if(!SecurityDesUtils.checkToken(uid, token)){
            throw new RuntimeException("token is not available");
        }*/
        //TODO 设置presence，验证登录token
        //entity.setPresence(presence);
        return   EntityImpl.parse(uid + EntityImpl.CHAR_AT +domain + EntityImpl.CHAR_SLASH + os);
    }

    @Override
    public boolean isBlankList(Entity form, Entity to) {
        BlackList fromBlacklist= blackListRepository.findOneBySenderAndReceiver(form.getNode(), to.getNode());
        BlackList toBlacklist= blackListRepository.findOneBySenderAndReceiver(to.getNode(),form.getNode());
        if(StringUtils.isEmpty(fromBlacklist) && StringUtils.isEmpty(toBlacklist)){
            return  false;
        }
        return true;
    }



    @Override
    public void isFromLocked(Entity entity) throws RuntimeException {
        String status=getStatus(entity);
        if(!status.equals(ApplicationCostant.BusinessConfig.USER_STATUS_IS_LOCKED) && !StringUtils.isEmpty(status)){
            return;
        }
        throw new RuntimeException("user is locked or not exists");
    }


    @Override
    public boolean isToLocked(Entity entity) {
        String status=getStatus(entity);
        if(!status.equals(ApplicationCostant.BusinessConfig.USER_STATUS_IS_LOCKED)){
            return false;
        }
        return true;
    }


    @Override
    public boolean isAdmin(Entity entity) {
        long  permission=0;
        User user=this.getUserInfo(entity);
        if(!StringUtils.isEmpty(user)){
            permission=Integer.valueOf(user.getPermission());
            if(ApplicationCostant.BusinessConfig.hasPermission(permission, ApplicationCostant.BusinessConfig.USER_PERMISSION_CHAT_IS_NOLIMIT)){
                return true;
            }
        }

         return false;
    }
}
